# constructProj/configTemplate.py
# -*-coding:utf-8 -*-
# @file       configTemplate.py
# @author     yearnext
# @version    1.0.0
# @date       2021-11-19
# @brief      constructProj configTemplate.py component
import datetime
import ui
import sys
import traceback
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *

def getDefaultTemplate():
    today = datetime.date.today()
    formatted_today = today.strftime('%y-%m-%d')

    dict = {'fileType': 'None',
            'path':'',
            'reposityUrl':'',
            'author':'',
            'version':'1.0',
            'createDate':formatted_today,
            'desc':''}
    return dict

def visualDefaultVal(val):
    widget = QLineEdit()
    widget.setText(val)
    widget.setStyleSheet("QLineEdit{font-family:\"Consolas\";background:transparent;border-width:0;border-style:outset}")

    return widget

def visualFileTypeVal(val):
    widget = QComboBox()
    widget.clear()
    widget.setStyleSheet("QComboBox{font-family:\"Consolas\";background:transparent;border-width:0;border-style:outset}")
    strList = [u'BoardManagementDesc', u'BoardDesc', u'SdkManagementDesc', u'SdkDesc', u'LibManagementDesc', u'LibDesc', u'None']
    widget.addItems(strList)
    widget.setCurrentText(u'None')

    for i in strList:
        if i == val:
            widget.setCurrentText(i)

    return widget

def visualPathTypeVal(val):
    try:
        widget = ui.filePathWidget(mode=1, path=val)
        return widget

    except Exception as e:
        # 访问异常的错误编号和详细信息
        print(traceback.print_exc(), sys._getframe().f_lineno, traceback.format_exc(), str(e), repr(e))

def visualNumTypeVal(val):
    widget = QSpinBox()
    widget.setValue(val)
    widget.setStyleSheet("QSpinBox{font-family:\"Consolas\";background:transparent;border-width:0;border-style:outset}")
    return widget

def visualStringTypeConfig(widget, key, val, rootNode=None):
    node = None
    valWidget = None

    if key == 'fileType':
        valWidget = visualFileTypeVal(val)
    elif key == 'path':
        valWidget = visualPathTypeVal(val)
    elif key == 'reposityUrl':
        valWidget = visualPathTypeVal(val)
    else:
        valWidget = visualDefaultVal(val)

    if rootNode is not None:
        node = QTreeWidgetItem(rootNode)
        node.setText(0, key)
        widget.setItemWidget(node, 1, valWidget)
    else:
        node = QTreeWidgetItem(widget)
        node.setText(0, key)
        widget.setItemWidget(node, 1, valWidget)

def visualIntTypeConfig(widget, key, val, rootNode=None):
    node      = None
    valWidget = None

    if key == 'num':
        valWidget = visualNumTypeVal(val)
    else:
        valWidget = visualNumTypeVal(val)

    if rootNode is not None:
        node = QTreeWidgetItem(rootNode)
        node.setText(0, key)
        widget.setItemWidget(node, 1, valWidget)
    else:
        node = QTreeWidgetItem(widget)
        node.setText(0, key)
        widget.setItemWidget(node, 1, valWidget)

def visualListTypeConfig(widget, key, val, rootNode=None):
    if rootNode is not None:
        node = QTreeWidgetItem(rootNode)
    else:
        node = QTreeWidgetItem(widget)

    node.setText(0, key)

    # # print(type(val[0]), val)
    # # 加载字典类型数据
    cnt = 0
    for i in val:
        if isinstance(i, dict):
            subNode = QTreeWidgetItem(node)
            subNode.setText(0, str('{0}Item{1}'.format(key, cnt+1)))
            visualConfigFile(widget, i, rootNode=subNode)
        # widget.setItemWidget(node, 1, node)

def visualConfigFile(widget, inData, rootNode=None):
    # 输入数据异常返回1
    if isinstance(inData, dict) == False:
        return 1

    # if exRootNode is None:
    #     rootNode = QTreeWidgetItem(widget)
    #     rootNode.setText(0, 'JSON Visual')

    # 遍历配置项
    for (key, value) in inData.items():
        if isinstance(value, str):
            # print('key:', key, 'value:', value, 'val type:', type(value))
            visualStringTypeConfig(widget, key, value, rootNode)
        elif isinstance(value, int):
            # print('key:', key, 'value:', value, 'val type:', type(value))
            visualIntTypeConfig(widget, key, value, rootNode)
        elif isinstance(value, list):
            # print('key:', key, 'value:', value, 'val type:', type(value))
            visualListTypeConfig(widget, key, value, rootNode)
        elif isinstance(value, tuple):
            print('key:', key, 'value:', value, 'val type:', type(value))
        elif isinstance(value, dict):
            print('key:', key, 'value:', value, 'val type:', type(value))
        else:
            print('key:', key, 'value:', value, 'val type:', type(value))

    if rootNode is None:
        # widget.addTopLevelItem(rootNode)
        widget.expandAll()
